﻿
Partial Class WebForm_ManageProgram_SearchProgram
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLParentProgram()
            RaiseEvent Loaded(Me, New EventArgs)

        End If
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindGV()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        Me.SelectedProgramID = Me.gv.DataKeys(e.NewEditIndex)("ProgramID")
        RaiseEvent Editing(sender, e)
    End Sub
End Class

Partial Class WebForm_ManageProgram_SearchProgram
    Private Sub BindDDLParentProgram()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select p.ProgramID, p.ProgramName ")
        sql.AppendFormat("from ( ")
        sql.AppendFormat("select ProgramGroupID = ProgramID from Program p ")
        sql.AppendFormat("where p.Parent=0 or p.ProgramID = 0 ")
        sql.AppendFormat("union select pg.ProgramGroupID ")
        sql.AppendFormat("from ProgramGroup pg ) pg ")
        sql.AppendFormat("inner join Program p on pg.ProgramGroupID = p.ProgramID ")
        sql.AppendFormat("order by p.Parent, p.Sequence ")

        Me.ddlProgramGroup.DataTextField = "ProgramName"
        Me.ddlProgramGroup.DataValueField = "ProgramID"
        Me.ddlProgramGroup.DataSource = dat.GetData(sql)
        Me.ddlProgramGroup.DataBind()

        'Me.ddlProgramGroup.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select p.*, p_parent.ProgramName as ParentProgramName ")
        sql.AppendFormat("from Program p ")
        sql.AppendFormat("inner join Program p_parent on p.Parent = p_parent.ProgramID ")
        sql.AppendFormat("where p.Editable='Y' ")
        sql.AppendFormat(Me.GetWhereSQL())
        'sql.AppendFormat("order by p_parent.ProgramID ")
        sql.AppendFormat("order by p.Sequence ")
        sql.AppendFormat("," + Me.gv.SortSQL)

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        If Me.ddlProgramGroup.SelectedValue <> "" Then
            sql.AppendFormat("and p.Parent = {0} ", Me.ddlProgramGroup.SelectedValue)
        End If

        Return sql.ToString()
    End Function

    Public Property SelectedProgramID() As String
        Get
            Return ViewState("SelectedProgramID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedProgramID") = value
        End Set
    End Property

    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)
    Public Event Editing(ByVal source As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
End Class

